/*
 * 验证算法
 * @author 彭海涛
 */
var Validate = (function() {

	var $ = {};

	/*
	 * 手机号验证
	 * @param {String} number  需要验证的手机号码
	 * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9]
	 * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
	 * 联通号段: 130,131,132,145,152,155,156,170,171,176,185,186
	 * 电信号段: 133,134,153,170,177,180,181,189
	 */
	var rPhoneNumber = /^1[3|5|7|8]\d{9}$/;
	$.isPhoneNumber = function(mNumber) {
		return rPhoneNumber.test(mNumber);
	}
	
	var phoneReg = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/; //手机号和座机号的正则
	$.isTelePhoneOrMobilePhone = function(mNumber) {
		return phoneReg.test(mNumber);
	}
	/*
	 * 邮箱验证
	 * @param {String} mMail  需要验证的邮箱
	 */
	var rEmail = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/;
	$.isEmail = function(mMail) {
		return rEmail.test(mMail);
	}

	/*
	 * 18位身份证号码验证
	 * @param {String} mId  需要验证的身份证号
	 */
	var rIdCard = /^(\d{6})([0-9]{4})(((0[13578]|1[02])(0[1-9]|[1|2][0-9]|3[01]))|((0[469]|11)(0[1-9]|[1|2][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8])))(\d{3})([0-9]|X)$/;
	$.isIdCard = function(mId) {
		return rIdCard.test(mId);
	}

	/*
	 * input禁止输入空格
	 * 监听方法onkeyup(this)
	 * @param {Object} mTarget 监听的对象
	 */
	$.banBlank = function(mTarget) {
		if(/^ +| +$/.test(mTarget.value)){
			mTarget.value = mTarget.value.replace(/^ +| +$/g, '');
		}
	}

	/*
	 * input只能输入数字,不包含小数点
	 * 监听方法onkeyup(this)
	 * @param {Object} oTarget 监听的对象
	 */
	$.onlyNum = function(oTarget) {
		if(/\D/.test(oTarget.value)){
			oTarget.value = oTarget.value.replace(/\D/g, '')
		}
	}
	
	/*
	 * input只能输入数字,包含小数点与负数
	 * 监听方法onkeyup(this)
	 * @param {Object} oTarget 监听的对象
	 */
	$.onlyMathNum = function(oTarget) {
		if(/[^\- \d.]/.test(oTarget.value)){
			oTarget.value = oTarget.value.replace(/[^\- \d.]/g,'')
		}
	}
	$.onlyTelephoneAndMobileNum = function(oTarget) {
		if(/[^\- \d]/.test(oTarget.value)){
			oTarget.value = oTarget.value.replace(/[^\- \d]/g,'')
		}
	}
    
    /*
	 * input只能输入数字与英文字母和下划线
	 * 监听方法onkeyup(this)
	 * @param {Object} oTarget 监听的对象
	 */
	$.onlyNumAndEnglish = function(oTarget) {
		oTarget.value = oTarget.value.replace(/[^\w\.]/ig,'')
	}
	
	/*
	 * input只能输入中文
	 * 监听方法onkeyup(this)
	 * @param {Object} oTarget 监听的对象
	 */
	$.onlyChinese = function(oTarget) {
		oTarget.value = oTarget.value.replace(/[^\u4E00-\u9FA5]/g,'')
	}
	
	
	/*
	 * 判断是否为字符串 
	 */

	$.isString = function(obj) {
		return typeof obj == 'string';
	}

	/*
	 * 判断是否为对象 
	 */
	$.isObject = function(obj) {
		return typeof obj == "object";
	}

	/*
	 * 判断是否为函数 
	 */
	$.isFunction = function(obj) {
		return typeof obj == "function";
	}

	/*
	 * 判断是否为JSON 
	 */
	$.isJson = function(obj) {
		return typeof(obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
	}

	/*
	 * 判断是否为空字符串 
	 */
	var rEmpty = /^\S+$/;
	$.isEmpty = function(value) {
		if(value == undefined) return false;
		if(value == null) return false;
		return rEmpty.test(value);
	}

/*
	 * input只能输入数字,包含小数点
	 * 监听方法onkeyup(this)
	 * @param {Object} oTarget 监听的对象
	 */
	$.onlyFFSMathNum = function(oTarget) {
		if(/[^\d.]/.test(oTarget.value)){
			oTarget.value = oTarget.value.replace(/[^\d.]/g,'')
		}
	}
    
	return $;
})();